//
//  PKGroupGroupLatestViewController.m
//  PianKe
//
//  Created by lanou on 15/10/17.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKGroupGroupLatestViewController.h"
#import "PKGroupGroupCellModel.h"
#import "PKGroupGroupCell.h"
#import "PKRequestManager.h"
@interface PKGroupGroupLatestViewController ()
@property(nonatomic,strong)PKGroupGroupCellModel *groupModel;
@property(nonatomic,strong)NSMutableArray *groupModelArray;

@end

@implementation PKGroupGroupLatestViewController

-(NSMutableArray *)groupModelArray
{
    if (_groupModelArray == nil) {
        _groupModelArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _groupModelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[PKGroupGroupCell class] forCellReuseIdentifier:@"groupCell"];
    
 
    [self requestData];
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"client"] = @"1";
    parDic[@"sort"] = @"addtime";
    parDic[@"limit"] = @"10";
    parDic[@"version"] = @"3.0.2";
    parDic[@"start"] = @"0";
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/group/group_list" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *listArray = dic[@"data"][@"list"];
        
        [self.groupModelArray removeAllObjects];
        
        for (NSDictionary *listDic in listArray) {
            _groupModel = [[PKGroupGroupCellModel alloc]init];
            [_groupModel setValuesForKeysWithDictionary:listDic];
            [self.groupModelArray addObject:_groupModel];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"----------------社区小组请求数据错误-------------");
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _groupModelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _groupModel = _groupModelArray[indexPath.row];
    
    PKGroupGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell" forIndexPath:indexPath];
    [cell configureGroupCell:_groupModel];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 180;
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
